import UI
from UI.SimpleTimeDomainViewDiag import Ui_SimpleTimeDomianViewDiag
from PyQt4 import QtCore, QtGui
from guiqwt.builder import make

import numpy
import collections



class SimpleTimeDomianView(QtGui.QDialog, Ui_SimpleTimeDomianViewDiag):
    def __init__(self, Buffer):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.curvewidget.register_all_curve_tools()
        self.Buffer = Buffer
        self.DataBuffer = {}
        self.CurveItem = {}
        i = 1
        while i < len(Buffer)+1:
            self.DataBuffer[i]=(collections.deque([],10))
            self.CurveItem[i] = make.curve([], [], color='r')
            self.curvewidget.plot.add_item(self.CurveItem[i])
            
            i = i + 1
        
        
        

        #self.curve_item = make.curve([], [], color='b')
        #self.curve_item2 = make.curve([], [], color='r')
        #self.curvewidget.plot.add_item(self.curve_item)
        #self.curvewidget.plot.add_item(self.curve_item2)
        
        self.RefreshRateEdit.setText("""%d ms"""%1000)
        self.RefreshRatems = float(self.RefreshRateEdit.text().replace("ms",""))
        self.connect(self.RefreshRateEdit, QtCore.SIGNAL("editingFinished()"), self.RefreshUpdate)

        self.AxisLimits = [0,50,0,1024]
        self.UpdateLimits()
        
        
        self.connect(self.AutoRangeCheck, QtCore.SIGNAL("stateChanged(int)"), self.SetAutoRange)
        
        self.Timer = QtCore.QTimer()
        self.Timer.setInterval(self.RefreshRatems) 
        self.Timer.setSingleShot(False)
        QtCore.QObject.connect(self.Timer, QtCore.SIGNAL("timeout()"), self.UpdatePlot)
        self.Timer.start()
        
    def SetLimits(self,row, col):
        if self.AutoRangeCheck.checkState() != 2:
            self.AxisLimits[row] = int(self.tableWidget.item(row, col).text())
            self.UpdateLimits()
        else:
            self.tableWidget.setItem(row, col, QtGui.QTableWidgetItem(""" """))
        
    def SetAutoRange(self):
        if self.AutoRangeCheck.checkState() == 2:
            self.disconnect(self.tableWidget, QtCore.SIGNAL("cellChanged(int, int)"), self.SetLimits)
        else:
            self.connect(self.tableWidget, QtCore.SIGNAL("cellChanged(int, int)"), self.SetLimits)
            
    def RefreshUpdate(self):
        Number, FloatCheck = self.RefreshRateEdit.text().replace("ms","").toFloat()
        if not FloatCheck:
            self.Message = QtGui.QMessageBox(self)
            self.Message.setText("Input must be of type float")
            self.Message.show()
        else:
            if Number < 0.0:
                Number = 1000.0
            self.RefreshRatems = Number
        self.RefreshRateEdit.setText("""%d ms"""%self.RefreshRatems)
        self.Timer.setInterval(self.RefreshRatems)

    def UpdateLimits(self):
        self.curvewidget.plot.set_plot_limits(self.AxisLimits[0],self.AxisLimits[1],self.AxisLimits[2],self.AxisLimits[3])
    
    
    def UpdatePlot(self):
        
        
        Channel = 1
        #while(self.tableWidget.rowCount() > 0):
        #    self.tableWidget.removeRow(0)
        for Data in self.Buffer:
            
            #self.tableWidget.insertRow(self.tableWidget.rowCount())
            #self.tableWidget.setItem(channel-1, 0, QtGui.QTableWidgetItem("""Channel %d"""%(Channel)))
            self.tableWidget.setItem(Channel-1, 0, QtGui.QTableWidgetItem("""%.2f"""%(Data)))
            
            #self.tableWidget.setCellWidget(self.tableWidget.rowCount()-1, 2, QtGui.QCheckBox())
            
            
            #self.tableWidget.cellWidget(self.tableWidget.rowCount()-1, 1).uniqueId = """%d"""%Dep
            #self.tableWidget.cellWidget(self.tableWidget.rowCount()-1, 1).clicked.connect(self.OnDeleteEvent)
            
            
            self.DataBuffer[Channel].append(Data)
            
            self.CurveItem[Channel].set_data(numpy.arange(0,len(self.DataBuffer[Channel])),self.DataBuffer[Channel])
            Channel = Channel + 1
            

        self.CurveItem[1].plot().replot()
        
        #Data = [0.0]
        #Data2 = [0.0]
        #
        #if self.Stream1Check.checkState():
        #    Data = self.Buffer1.GetBuffer()
        #    if self.checkBoxLOG.checkState():
        #        Data = 10.0 * numpy.log10(Data)
        #self.curve_item.set_data(numpy.arange(0,len(Data)),Data)
        #
        #if self.Stream2Check.checkState():
        #    Data2 = self.Buffer2.GetBuffer()
        #    if self.checkBoxLOG.checkState():
        #        Data = 10.0 * numpy.log10(Data)
        #self.curve_item2.set_data(numpy.arange(0,len(Data2)),Data2)
        #
        #self.curve_item.plot().replot()
        #
        #if self.AutoRangeCheck.checkState():
        #    self.AxisLimits[3] = (1.3 * numpy.max(numpy.array([numpy.max(Data),numpy.max(Data2)]))) + 1.0
        #    self.AxisLimits[2] = (1.3 * numpy.min(numpy.array([numpy.min(Data),numpy.min(Data2)]))) + 1.0
        #    i = 0
        #    while i < 4:
        #        self.tableWidget.setItem(i, 0, QtGui.QTableWidgetItem("""%d"""%int(self.AxisLimits[i])))
        #        i = i + 1
        #    
        #    self.UpdateLimits()
        #    

                






if __name__ == "__main__":
    import sys
    B = [0,0,0,0,0,0]
    app = QtGui.QApplication(sys.argv)
    SimpleTimeDomianView = QtGui.QDialog()
    ui = Ui_SimpleTimeDomianView(B)
    ui.setupUi(SimpleTimeDomianView)
    SimpleTimeDomianView.show()
    sys.exit(app.exec_())